package com.ll.mapdemo;

import java.util.*;

/**
 * @author 刘龙
 * @version 1.0
 */
public class MapFor {
    public static void main(String[] args) {
        HashMap hashMap = new HashMap();
        hashMap.put("马云", "阿里巴巴");
        hashMap.put("刘强东", "京东");
        hashMap.put(null, "网易");
        hashMap.put("罗永浩", null);

        // 第一组: 先取出所有key, 通过key, 取出对应value
        Set keySet = hashMap.keySet();
        // (1) 增强for
        for (Object key : keySet) {
            System.out.println(key + "-" + hashMap.get(key));
        }

        // (2) 迭代器
        Iterator iterator = keySet.iterator();
        while (iterator.hasNext()) {
            Object key = iterator.next();
            System.out.println(key + "-" + hashMap.get(key));
        }

        // 第二组: 把所有的value取出
        Collection values = hashMap.values();
        // (1) 增强for
        for (Object value : values) {
            System.out.println(value);
        }

        // (2) 迭代器
        Iterator iterator1 = values.iterator();
        while (iterator1.hasNext()) {
            Object next = iterator1.next();
            System.out.println(next);
        }

        // (3) 通过EntrySet 来获取 k-v
        Set entrySet = hashMap.entrySet();
        // (1) 增强for
        for (Object entry : entrySet) {
            // 将entry 转成 Map.Entry
            Map.Entry m = (Map.Entry) entry;
            System.out.println(m.getKey() + "-" + m.getValue());
        }

        // (2) 迭代器
        Iterator iterator2 = entrySet.iterator();
        while (iterator2.hasNext()) {
            Object next = iterator2.next();
            // 向下转型 Map.Entry
            Map.Entry m =  (Map.Entry) next;
            System.out.println(m.getKey() + "-" + m.getValue());
        }
    }
}
